/**
 * Created by yindume on 2018/12/10.
 */
var express = require('express');
var router = express.Router();
var db = require('../../../libs/mysql');
//统一返回格式
var res_Data;
router.use( function(req, res, next) {
    res_Data = {
        code: 0,
        message: ''
    }
    next();
});
router.get('/',function (req,res) {
    db.query(`SELECT * FROM category`,(err,data)=>{
        if(err){
            res.send('查询分类').end();
        }else {
            res.render('admin/content/add', {
                userInfo:req.userInfo,
                category:data
            });
        }
    })
});

router.post('/',function(req,res){
    var categoryid = req.body.category;//分类
    var title = req.body.title;//标题
    var userid = req.userInfo.id;
    var abstract = req.body.abstract;//简介
    var content = req.body.editorValue;//内容

    db.query(`INSERT INTO contents ( categoryid, title, userid , abstract , content )
                       VALUES
                       ( '${categoryid}', '${title}','${userid}','${abstract}','${content}' );`,(err,data)=>{
        if(err){
            res.send('插入内容错误').end();
        }else {
            res_Data.message = '提交成功';
            res.json(res_Data);
            return;
        }
    })

})



module.exports = router;